package com.iris.live.services.data.repositories.impl;

import com.google.common.collect.Maps;
import com.iris.live.services.data.repositories.AccountRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xin on 2016/8/9.
 */
@Repository
@Transactional//(readOnly = true)
public class AccountRepositoryImpl extends BaseRepository implements AccountRepository {

    @Override
    public List getAccessibleItems(String permission, String username) {
        // distinct itemInfo
        String sql = "select i.* from itemInfo i\n" +
                "inner join (\n" +
                "select\n" +
                "       distinct ri.itemId\n" +
                "    from\n" +
                "        userRole ur \n" +
                "    left join\n" +
                "        roleInfo r \n" +
                "            on ur.roleid = r.id \n" +
                "    left join\n" +
                "        roleItem ri \n" +
                "            on r.id = ri.roleId \n" +
                "    \n" +
                "    where\n" +
                "        ur.permission = :permission\n" +
                "        and ur.username = :username\n" +
                ") as q on i.id = q.itemId";
        Map params = Maps.newHashMap();
        params.put("permission", permission);
        params.put("username", username);
        return this.getList(sql, params);
    }

    @Override
    public List getDuty(String permission) {
        String sql = "select a.id,a.roleName from roleInfo a \n";
        sql += "join userRole b on a.id = b.roleId \n";
        sql += "where b.permission = :permission \n";
        sql += "order by a.id";
        Map params = new HashMap();
        params.put("permission",permission);
        return this.getList(sql,params);
    }

    @Override
    public List getAllSale(String permit) {
        String sql =
                "select 许可," +
                "   用户名," +
                "   人员姓名, " +
                "   品牌 " +
                "from 用户 " +
                "where 许可 = :permit " +
                "and 角色 = '销售顾问' " +
                "and 接待状态 = '是' " +
                "and 删除状态 <> '已删除' " +
                "order by 人员姓名 ";
        Map params = new HashMap();
        params.put("permit", permit);
        return this.getList(sql,params);
    }
}
